package com.example.nsl.behavior;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewTreeObserver;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.resources.MaterialResources;

public class IamgeBavior extends ViewOffsetBehavior {

    public IamgeBavior(){
    }

    public IamgeBavior(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void layoutChild(@NonNull CoordinatorLayout parent, @NonNull View child, int layoutDirection) {
//        super.layoutChild(parent, child, layoutDirection);
//        Rect rect=new Rect();
        for (int i=0; i<parent.getChildCount(); i++){
            View view=parent.getChildAt(i);

            if(view instanceof RecyclerView){
                RecyclerView childrecyclerview= (RecyclerView) view;
                int [] post=new int[2];
                childrecyclerview.getLocationOnScreen(post);

                Rect rectImage=new Rect();

                rectImage.left= childrecyclerview.getMeasuredWidth();

                rectImage.top=post[1];
                if (ViewCompat.getFitsSystemWindows(parent)) {
                    @SuppressLint("RestrictedApi")
                    final WindowInsetsCompat parentInsets = parent.getLastWindowInsets();
                    rectImage.top= rectImage.top+ parentInsets.getSystemWindowInsetTop()
                            + parentInsets.getSystemWindowInsetBottom()+(childrecyclerview.getMeasuredHeight()-child.getMeasuredHeight())/2;
                }else{
                    rectImage.top= rectImage.top+(childrecyclerview.getMeasuredHeight()-child.getMeasuredHeight())/2;
                }
                rectImage.right=child.getMeasuredWidth()+rectImage.left;
                rectImage.bottom=child.getMeasuredHeight()+rectImage.top;
                //int l, int t, int r, int b
                child.layout(rectImage.left,rectImage.top,rectImage.right,rectImage.bottom);


                Log.e("ImageBehavior","" + post[0]+ "_" +post[1] + rectImage);
//                Log.e("ImageBehavior",""+childrecyclerview.getClipBounds());
                break;
            }
        }
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                                       @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
//        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type);
        return axes == ViewCompat.SCROLL_AXIS_HORIZONTAL;
    }


    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                               @NonNull View target,
                               int dxConsumed, int dyConsumed,
                               int dxUnconsumed, int dyUnconsumed,
                               int type, @NonNull int[] consumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed);
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child,
                                  @NonNull View target, int dx, int dy,
                                  @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);


        Log.e("ImageBehavior","---------------  ： " + dx);
        Log.e("ImageBehavior","==layoutChild 1  == " + child.getLeft());
        Log.e("ImageBehavior","==layoutChild 2  == " + target.getLeft());
        Log.e("ImageBehavior","==layoutChild 3 == " + getLeftAndRightOffset());

//        setLeftAndRightOffset(-300);
        if(child.getVisibility()==View.VISIBLE){// 必须是显示情况下，才能滑动
            if(target instanceof RecyclerView){
                RecyclerView recyclerview= (RecyclerView) target;
                if(recyclerview.getLeft()<0 && dx>0){ //向左滑动的时候
                    int mdesjl=-child.getMeasuredWidth();
                    if(getLeftAndRightOffset() == mdesjl){
                        return;
                    }
                    if((getLeftAndRightOffset()-dx)< mdesjl){  // -96- 8  100
                        setLeftAndRightOffset(mdesjl);
                        consumed[0]=mdesjl-getLeftAndRightOffset();
                    }else{
                        setLeftAndRightOffset(getLeftAndRightOffset()-dx);
                        consumed[0]=dx;
                    }
                }else  if( dx < 0){ //向右边滑动的哦
                    Log.e("ImageBehavior","==layoutChild  6  == " + getLeftAndRightOffset() + "" + target.getLeft());

                }

            }
        }

    }
}
